package com.yuanrui.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.yuanrui.auth.application.convert.AuthRolePermissionConverter;
import com.yuanrui.auth.application.dto.AuthRolePermissionDTO;
import com.yuanrui.auth.common.entity.Result;
import com.yuanrui.auth.domain.entity.AuthRolePermissionBO;
import com.yuanrui.auth.domain.service.AuthRolePermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色权限控制器
 */
@RestController
@Slf4j
@RequestMapping("/rolePermission/")
public class RolePermissionController {

    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 添加权限
     */
    @RequestMapping("add")
    public Result<Boolean> addRole(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("RolePermissionController.add：{}", JSON.toJSONString(authRolePermissionDTO));
            }
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "角色ID不能为空");
            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionConverter.INSTANCE.convertToBO(authRolePermissionDTO);
            Boolean addPermission = authRolePermissionDomainService.addRolePermission(authRolePermissionBO);
            return Result.ok(addPermission);
        } catch (Exception e) {
            log.error("RolePermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail("添加角色权限失败");
        }
    }
}
